source('604974235_stats102a_hw1.R')
To compute the GCD, our pseudocode is as follows
- for two numbers x and y, while y is not zero then
- if x is greater than y
- x = x-y
- if y is greater than x
- y = y-x
- when y reaches zero, output the value of x as the GCD
To compute the GCD for three numbers, x, y, and z
- compute the GCD for y and z, using the GCD algorithm
- store the gcd of y and z as a third number, temp
- compute the gcd of x and temp, which is the gcd of the three numbers, x, y, and z.
gcd(180,25)
## [1] 5
gcd_3(180, 50, 25)
## [1] 5
Flowcharts for the gcd() and gcd_3() functions.
Here I demonstrate my get factors function
get_factors(24)
## [1] 2 3 4 6 8 12 24
x <- sample(x = 1e4, size = 1)
y <- get_factors(x)
this_works <- prod(y) == x & all(is_prime(y))
The pseudocode for the is_prime function is
-iterate from 2 to one less than the input number
-check if the input number is divisible by the iterating number
-if no numbers from 2 to one less than the input number divide the input number, the input number is prime
The pseudocode for the get_factors function is
-iterate from 2 to the input number
-if any number between that range is both prime (check using is_prime function), and is a factor of the input number, it is a prime factor
-append that number into the output, factors vector.